SET(Lua_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/install")

SET(Lua_INSTALL_BIN_SUBDIR "bin" CACHE PATH
  "Install dir for binaries (relative to Lua_INSTALL_PREFIX)")

SET(Lua_INSTALL_LIB_SUBDIR "lib" CACHE PATH
  "Install dir for archives (relative to Lua_INSTALL_PREFIX)")

SET(Lua_INSTALL_INCLUDE_SUBDIR "include" CACHE PATH
  "Install dir for include (relative to Lua_INSTALL_PREFIX)")

SET(Lua_INSTALL_CMAKE_SUBDIR "share/lua/cmake" CACHE PATH
  "Install dir for .cmake files (relative to Lua_INSTALL_PREFIX)")

SET(Lua_INSTALL_LUA_PATH_SUBDIR "share/lua/5.1" CACHE PATH
  "Install dir for Lua packages files (relative to Lua_INSTALL_PREFIX)")

SET(Lua_INSTALL_LUA_CPATH_SUBDIR "lib/lua/5.1" CACHE PATH
  "Install dir for Lua C packages files (relative to Lua_INSTALL_PREFIX)")

SET(Lua_INSTALL_FINDLUA_DIR "${Lua_BINARY_DIR}/cmake")
SET(Lua_INSTALL_BIN "${Lua_INSTALL_PREFIX}/${Lua_INSTALL_BIN_SUBDIR}")
SET(Lua_INSTALL_LIB "${Lua_INSTALL_PREFIX}/${Lua_INSTALL_LIB_SUBDIR}")
SET(Lua_INSTALL_INCLUDE "${Lua_INSTALL_PREFIX}/${Lua_INSTALL_INCLUDE_SUBDIR}")
SET(Lua_INSTALL_CMAKE "${Lua_INSTALL_PREFIX}/${Lua_INSTALL_CMAKE_SUBDIR}")
SET(Lua_INSTALL_LUA_PATH "${Lua_INSTALL_PREFIX}/${Lua_INSTALL_LUA_PATH_SUBDIR}")
SET(Lua_INSTALL_LUA_CPATH "${Lua_INSTALL_PREFIX}/${Lua_INSTALL_LUA_CPATH_SUBDIR}")

# reverse relative path to prefix (ridbus is the palindrom of subdir)
FILE(RELATIVE_PATH Lua_INSTALL_BIN_RIDBUS "${Lua_INSTALL_BIN}" "${Lua_INSTALL_PREFIX}/.")
FILE(RELATIVE_PATH Lua_INSTALL_CMAKE_RIDBUS "${Lua_INSTALL_CMAKE}" "${Lua_INSTALL_PREFIX}/.")
GET_FILENAME_COMPONENT(Lua_INSTALL_BIN_RIDBUS "${Lua_INSTALL_BIN_RIDBUS}" PATH)
GET_FILENAME_COMPONENT(Lua_INSTALL_CMAKE_RIDBUS "${Lua_INSTALL_CMAKE_RIDBUS}" PATH)

IF(UNIX)
  OPTION(Lua_BUILD_WITH_RPATH "Build libraries with rpaths" ON)

  IF(Lua_BUILD_WITH_RPATH)
    SET(CMAKE_SKIP_BUILD_RPATH FALSE)
    SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
    SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
    FILE(RELATIVE_PATH Lua_INSTALL_BIN2LIB
      "${Lua_INSTALL_BIN}" "${Lua_INSTALL_LIB}")
    FILE(RELATIVE_PATH Lua_INSTALL_BIN2CPATH
      "${Lua_INSTALL_BIN}" "${Lua_INSTALL_LUA_CPATH}")
    IF(NOT APPLE)
      OPTION(WITH_DYNAMIC_RPATH
        "Build libraries with executable relative rpaths (\$ORIGIN)" ON )
    ENDIF(NOT APPLE)
    IF (WITH_DYNAMIC_RPATH OR APPLE)
      SET(CMAKE_INSTALL_RPATH "\$ORIGIN/${Lua_INSTALL_BIN2LIB}")
    ELSE (WITH_DYNAMIC_RPATH OR APPLE)
      SET(CMAKE_INSTALL_RPATH "${Lua_INSTALL_LIB}")
    ENDIF (WITH_DYNAMIC_RPATH OR APPLE)
    SET(CMAKE_INSTALL_NAME_DIR "@executable_path/${Lua_INSTALL_BIN2LIB}")
  ENDIF(Lua_BUILD_WITH_RPATH)

ENDIF(UNIX)

IF (WIN32)
  SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
  SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
ENDIF (WIN32)
